[][src]Crate dyn_cache

Caches for storing the results of repeated function calls. The cache types available use minimal dynamic dispatch to allow storing arbitrarily many types of query results in a single parent store.

There are two main flavors of cache available for use in this crate:

Shared typeSynchronized?
sync::SharedSendCacheMutex
local::SharedLocalCacheRefCell

These variants are used by calling sync::SharedSendCache::cache_with or local::SharedLocalCache::cache.

The shared cache types above are implemented by wrapping these "inner" types:

Mutable typeRequires Send?
sync::SendCacheyes
local::LocalCacheno

These "inner" caches require mutable access to call their functions like local::LocalCache::get which returns either a reference or a CacheMiss that can be passed back to the cache in local::LocalCache::store to initialize a value in the cache.

See sync::SendCache::get and sync::SendCache::store for the thread-safe equivalents.

The shared variants are defined by wrapping these inner cache types in reference counting and synchronized mutability.

Query types

Each query type maps to a typed "namespace" within the unityped cache storage, each query having a distinct type each for its scope, input, and output.

Scopes

The scope of a query is its identifier within cache storage. Scopes must implement Eq and Hash so that results can be efficiently and uniquely indexed within a namespace.

Each scope identifies 0-1 (Input, Output) pairs in each namespace. The same type of scope can be used in multiple queries without collision if the types of inputs, outputs, or both differ.

Inputs

The input to a query determines when it is (re-)run. If a given query has been run before, then the previous input is compared to the current input before potentially running the query. If the input hasn't changed, the query can be skipped and its previously-stored output is returned.

Outputs

The only constraint on query outputs is that they are owned (Output: 'static). This imposes the inconvenient requirement that all access to stored values occurs during the scope of a closure (similar to thread-locals in the standard library).

The most common way to work around this requirement is to choose output types that cheaply implement std::clone::Clone.

Allocations

In order to store distinct query results in the same container, allocations and indirection are required.

Borrowed query parameters

All of the cache functions accept a reference to a type Key: ToOwned<Owned=Scope> so that the scope is only cloned on the first insertion to its storage and all subsequent lookups can be with a borrowed type.

Like the query scope, functions to get cache values accept a borrowed version of the input and only clone it when the input has changed.

Causes

There are three situations where these caches allocate:

  1. caching new types which haven't been seen by that cache instance yet
  2. storing the results of a new query
  3. updating the results of a stored query

There are several types of allocations performed by the caches in this crate:

AllocationCauses
box a new, empty namespace(1)
resize a cache's map of namespaces(1)
call .to_owned() on a scope/key(2)
resize a namespace's storage(2)
call .to_owned() on an input/arg(2), (3)

Outside of these, only user-defined functions should perform any allocation.

Garbage Collection

Every value in the cache has a "liveness" which is set to "alive" when the value is first stored and again when it is read.

The inner caches offer local::LocalCache::gc and sync::SendCache::gc which are also exposed through local::SharedLocalCache::gc and sync::SharedSendCache::gc. When called, the gc() method retains only those values which are still "alive" and then marks them all "dead".

This behavior resembles a simple mark-and-sweep garbage collector where the "mark phase" is the use of the cache in between gc() calls. Any values which weren't used in the mark phase are dropped in the next "sweep phase" when gc() is called.

Nested Queries

While it is possible to nest use of the shared caches within the init closures passed to them, the caches do not yet track the required dependency relationship to correctly retain intermediate cached results across GCs. While this works well enough for some scenarios it needs to be resolved in the general case before this way of using this crate is recommended.

Modules

local

A cache for types which are not thread-safe (?Send).

sync

A thread-safe cache which requires stored types implement Send.

Structs

CacheEntry

A fully-initialized input/output pair, ready to be written to the store.

CacheMiss

The result of a failed attempt to retrieve a value from the cache. Initialize a full CacheEntry for storage with CacheMiss::init.